/**
 * @FileName: Preferences.java
 * @Package:com.bdtop.dbl.utils
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-16
 * @version V1.0
 */

package com.bdtop.dbl.utils;

import java.io.File;
import java.util.List;

import org.litepal.crud.DataSupport;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.bdtop.dbl.db.model.DBLDeviceInfo;
import com.bdtop.dbl.db.model.RecordInfo;
import com.bdtop.dbl.db.model.UserInfo;

/**
 * @ClassName: Preferences
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-16
 */
public class Preferences {

	// Preference key
	private static final String IS_FIRST_RUNNING = "is_first_running";
	private static final String REMEMBER_PWD = "remember_pwd";
	private static final String AUTO_LOGIN = "auto_login";
	private static final String REMEMBERED_USERNAME = "remembered_username";
	private static final String REMEMBERED_PWD = "remembered_pwd";
	private static final String LOGIN_SIGN = "login_sign";
	private static final String SELF_POSITION = "self_position";
	private static final String SELF_CITY_CODE = "self_city_code";
	private static final String SELF_LOCATION_INFO = "self_location_info";

	// private static final String VISITOR_MODE = "visitor_Mode";
	private static final String CURRENT_BLE_MAC = "current_ble_mac";
	private static final String CURRENT_BLE_NAME = "current_ble_name";

	// private static final String CURRENT_BLE_DISTANCE =
	// "current_ble_distance";
	// private static final String CURRENT_BLE_ALARM_TIME =
	// "current_ble_alarm_time";
	private static final String RESTART_BLE_NAME_AFTER_EXIT_APP = "restart_ble_name_After_exit_app";
	private static final String USER_IMG = "u";
	// private static final String USER_IMG_NAME = "user_img_name";
	private static final String DEVICE_IMG = "d";
	// private static final String DEVICE_IMG_NAME = "device_img_name";
	private static final String VERSION = "version";
	private static final String DOWNURL = "downurl";
	// private static final String SHOULD_REMIND_DOWN_MAP =
	// "should_remind_down_map";
	private static final String ISLOGIN = "ISLOGIN";
	private static final String FROM_PUSH = "from_push";
	private static final String IS_BIND_CID = "is_bind_cid";
	private static final String LAST_LOGIN_USER = "last_login_user";

	private static final String IMEI_FOR_MAC = "imei_for_mac_";
	private static final String MAC_FOR_IMEI = "mac_for_imei_";
	private static final String REBIND_IMEI = "rebind_imei";

	private static final String REMIND_DOWN_MAP_TIME = "remind_down_map_time";

	private static final String REGSITER_TO_LOGIN = "regsiter_to_login";

	public static SharedPreferences getPreferences(Context context) {
		// return PreferenceManager.getDefaultSharedPreferences(context);
		return context.getSharedPreferences(Constants.SP_PATH, Context.MODE_PRIVATE);
	}

	/**
	 * Get the first running flag from shared preferences, true is default.
	 * 
	 * @param @param context
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public static boolean isFirstRunning(Context context) {
		// return getPreferences(context).getBoolean(IS_FIRST_RUNNING, true);
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.isFirstRunning();
	}

	/**
	 * Set the first running flag into shared preferences.
	 * 
	 * @param @param context
	 * @param @param isFirstRunning
	 * @return void
	 * @throws
	 */
	public static void setFirstRunning(Context context, boolean isFirstRunning) {
		// getPreferences(context).edit().putBoolean(IS_FIRST_RUNNING,
		// isFirstRunning).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setFirstRunning(isFirstRunning);
		recordInfo.save();
	}

	// Save & Get remember password flag.
	public static boolean isRememberPwd(Context context) {
		// return getPreferences(context).getBoolean(REMEMBER_PWD, false);
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.isRememberPwd();
	}

	public static void setRememberPwd(Context context, boolean rememberPwd) {
		// getPreferences(context).edit().putBoolean(REMEMBER_PWD,
		// rememberPwd).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setRememberPwd(rememberPwd);
		recordInfo.save();
	}

	// Save & Get auto login flag.
	public static boolean isAutoLogin(Context context) {
		// return getPreferences(context).getBoolean(AUTO_LOGIN, false);
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.isAutoLogin();
	}

	public static void setAutoLogin(Context context, boolean autoLogin) {
		// getPreferences(context).edit().putBoolean(AUTO_LOGIN,
		// autoLogin).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setAutoLogin(autoLogin);
		recordInfo.save();
	}

	// Save & Get the password by user.
	public static void storePwdByUser(Context context, String userName, String pwd) {
		// getPreferences(context).edit().putString(userName, pwd).commit();
		List<UserInfo> userInfoList = DataSupport.where("username = ?", userName).find(UserInfo.class);
		if (userInfoList != null && userInfoList.size() > 0) {
			UserInfo userInfo = userInfoList.get(0);
			ContentValues values = new ContentValues();
			values.put("password", pwd);
			DataSupport.update(UserInfo.class, values, userInfo.getId());
		} else {
			UserInfo userInfo = new UserInfo();
			userInfo.setUsername(userName);
			userInfo.setPassword(pwd);
			userInfo.save();
		}
	}

	public static String getPwdByUser(Context context, String userName) {
		// return getPreferences(context).getString(userName, "");
		List<UserInfo> userInfoList = DataSupport.where("username = ?", userName).find(UserInfo.class);
		if (userInfoList != null && userInfoList.size() > 0) {
			UserInfo userInfo = userInfoList.get(0);
			String password = userInfo.getPassword();
			if (!TextUtils.isEmpty(password)) {
				return password;
			}
		}

		return "";
	}

	// Save & Get user name(phone number).
	public static String getUserName(Context context) {
		// return getPreferences(context).getString(REMEMBERED_USERNAME, "");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String rememberedUsername = recordInfo.getRememberedUsername();
		if (!TextUtils.isEmpty(rememberedUsername)) {
			return rememberedUsername;
		}
		return "";
	}

	public static void saveUserName(Context context, String userName) {
		// getPreferences(context).edit().putString(REMEMBERED_USERNAME,
		// userName).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setRememberedUsername(userName);
		recordInfo.save();
	}

	// Save & Get password.
	public static String getPassword(Context context) {
		// String pwd = getPreferences(context).getString(REMEMBERED_PWD, "");
		// return pwd;
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String rememberedPwd = recordInfo.getRememberedPwd();
		if (!TextUtils.isEmpty(rememberedPwd)) {
			return rememberedPwd;
		}
		return "";
	}

	public static void savePassword(Context context, String pwd) {
		// getPreferences(context).edit().putString(REMEMBERED_PWD,
		// pwd).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setRememberedPwd(pwd);
		recordInfo.save();
	}

	// Save & Get login sign.
	public static String getLoginSign(Context context) {
		// return getPreferences(context).getString(LOGIN_SIGN, "");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String loginSign = recordInfo.getLoginSign();
		if (!TextUtils.isEmpty(loginSign)) {
			return loginSign;
		}
		return "";
	}

	public static void saveLoginSign(Context context, String loginSign) {
		// getPreferences(context).edit().putString(LOGIN_SIGN,
		// loginSign).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setLoginSign(loginSign);
		recordInfo.save();
	}

	public static void saveLastSelfLocation(Context context, String[] lat_lng) {
		// getPreferences(context).edit().putString(SELF_POSITION, lat_lng[0] +
		// "#" + lat_lng[1]).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setLat(lat_lng[0]);
		recordInfo.setLng(lat_lng[1]);
		recordInfo.save();
	}

	public static String[] getLastSelfLocation(Context context) {
		// String latlng = getPreferences(context).getString(SELF_POSITION, "");
		// if (TextUtils.isEmpty(latlng))
		// return null;
		//
		// return new String[] { latlng.split("#")[0], latlng.split("#")[1] };

		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String lat = recordInfo.getLat();
		String lng = recordInfo.getLng();
		if (TextUtils.isEmpty(lat) && TextUtils.isEmpty(lng)) {
			return null;
		}

		return new String[] { lat, lng };
	}

	public static void saveLocationInfo(Context context, String locationInfo) {
		// getPreferences(context).edit().putString(SELF_LOCATION_INFO,
		// locationInfo).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setSelfLocationInfo(locationInfo);
		recordInfo.save();
	}

	public static String getLocationInfo(Context context) {
		// return getPreferences(context).getString(SELF_LOCATION_INFO, "");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String selfLocationInfo = recordInfo.getSelfLocationInfo();
		if (!TextUtils.isEmpty(selfLocationInfo)) {
			return selfLocationInfo;
		}
		return "";
	}

	public static void saveLastCityCode(Context context, String cityCode) {
		// getPreferences(context).edit().putString(SELF_CITY_CODE,
		// cityCode).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setSelfCityCode(cityCode);
		recordInfo.save();
	}

	public static String getLastCityCode(Context context) {
		// return getPreferences(context).getString(SELF_CITY_CODE, "");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String cityCode = recordInfo.getSelfCityCode();
		if (!TextUtils.isEmpty(cityCode)) {
			return cityCode;
		}
		return "";
	}

	// public static void saveVisitorMode(Context context, boolean
	// isVisitorMode) {
	// getPreferences(context).edit().putBoolean(VISITOR_MODE,
	// isVisitorMode).commit();
	// }
	//
	// public static boolean getVisitorMode(Context context) {
	// return getPreferences(context).getBoolean(VISITOR_MODE, true);
	// }

	public static void saveCurrentBleMac(Context context, String mac) {
		// getPreferences(context).edit().putString(CURRENT_BLE_MAC,
		// mac).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setCurrentBleMac(mac);
		recordInfo.save();
	}

	public static String getCurrentBleMac(Context context) {
		// return getPreferences(context).getString(CURRENT_BLE_MAC, "");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String currentBleMac = recordInfo.getCurrentBleMac();
		if (!TextUtils.isEmpty(currentBleMac)) {
			return currentBleMac;
		}
		return "";
	}

	public static void saveCurrentBleName(Context context, String name) {
		// getPreferences(context).edit().putString(CURRENT_BLE_NAME,
		// name).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setCurrentBleName(name);
		recordInfo.save();
	}

	public static String getCurrentBleName(Context context) {
		// return getPreferences(context).getString(CURRENT_BLE_NAME, "");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String currentBleName = recordInfo.getCurrentBleName();
		if (!TextUtils.isEmpty(currentBleName)) {
			return currentBleName;
		}
		return "";
	}

	// public static void saveCurrentBleDistance(Context context, float
	// distance) {
	// String bleName = getCurrentBleName(context);
	// getPreferences(context).edit().putFloat(CURRENT_BLE_DISTANCE + bleName,
	// distance).commit();
	// }
	//
	// public static float getCurrentBleDistance(Context context) {
	// String bleName = getCurrentBleName(context);
	// return getPreferences(context).getFloat(CURRENT_BLE_DISTANCE + bleName,
	// 5f);
	// }
	//
	// public static void saveCurrentBleAlarmTime(Context context, long time) {
	// String bleName = getCurrentBleName(context);
	// getPreferences(context).edit().putLong(CURRENT_BLE_ALARM_TIME + bleName,
	// time).commit();
	// }
	//
	// public static long getCurrentBleAlarmTime(Context context) {
	// String bleName = getCurrentBleName(context);
	// return getPreferences(context).getLong(CURRENT_BLE_ALARM_TIME + bleName,
	// 5000l);
	// }

	public static void saveLastConnectBluetoothDeviceAddress(Context context, String mBluetoothDeviceAddress) {
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setRestartBleNameAfterExitApp(mBluetoothDeviceAddress);
		recordInfo.save();
	}

	public static String getLastConnectBluetoothDeviceAddress(Context context) {
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String restartBleNameAfterExitApp = recordInfo.getRestartBleNameAfterExitApp();
		if (!TextUtils.isEmpty(restartBleNameAfterExitApp)) {
			return restartBleNameAfterExitApp;
		}
		return "";
	}

	public static void saveUserImg(Context context, String userImgBase64, String username) {
		// getPreferences(context).edit().putString(username + "_" + USER_IMG,
		// userImgBase64).commit();
		// fix : 图片存在指定文件里
		String fileName = username + "_" + USER_IMG + ".txt";
		File file = CacheUtil.getDiskHeadFileDir(context, fileName);
		boolean result = FileUtil.inputStreamToFile(FileUtil.byteToInputStream(userImgBase64.getBytes()), file);
		DBLDebug.logd("save user image :" + (result ? "success" : "fail"));
	}

	public static String getUserImgBase64(Context context, String username) {
		// return getPreferences(context).getString(username + "_" + USER_IMG,
		// "");
		// fix : 图片存在指定文件里
		String fileName = username + "_" + USER_IMG + ".txt";
		File file = CacheUtil.getDiskHeadFileDir(context, fileName);
		String result = FileUtil.FileToString(file);
		DBLDebug.logd("get user image :" + (!TextUtils.isEmpty(result) ? "success" : "no data"));
		return result;
	}

	public static void saveDeviceImg(Context context, String deviceImgBase64, String deviceImei) {
		// getPreferences(context).edit().putString(deviceImei + "_" +
		// DEVICE_IMG, deviceImgBase64).commit();
		// fix : 图片存在指定文件里
		String fileName = deviceImei + "_" + DEVICE_IMG + ".txt";
		File file = CacheUtil.getDiskHeadFileDir(context, fileName);
		boolean result = FileUtil.inputStreamToFile(FileUtil.byteToInputStream(deviceImgBase64.getBytes()), file);
		DBLDebug.logd("save device image :" + (result ? "success" : "fail"));
	}

	public static String getDeviceImgBase64(Context context, String deviceImei) {
		// return getPreferences(context).getString(deviceImei + "_" +
		// DEVICE_IMG, "");
		// fix : 图片存在指定文件里
		String fileName = deviceImei + "_" + DEVICE_IMG + ".txt";
		File file = CacheUtil.getDiskHeadFileDir(context, fileName);
		String result = FileUtil.FileToString(file);
		DBLDebug.logd("get device image :" + (!TextUtils.isEmpty(result) ? "success" : "no data"));
		return result;
	}

	public static void saveVersion(Context context, String version) {
		// getPreferences(context).edit().putString(VERSION, version).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setVersion(version);
		recordInfo.save();
	}

	public static String getVersion(Context context) {
		// return getPreferences(context).getString(VERSION, "100");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String version = recordInfo.getVersion();
		if (!TextUtils.isEmpty(version)) {
			return version;
		}
		return "100";
	}

	public static void saveDownurl(Context context, String downurl) {
		// getPreferences(context).edit().putString(DOWNURL, downurl).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setDownurl(downurl);
		recordInfo.save();
	}

	public static String getDownurl(Context context) {
		// return getPreferences(context).getString(DOWNURL, "");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String downurl = recordInfo.getDownurl();
		if (!TextUtils.isEmpty(downurl)) {
			return downurl;
		}
		return "";
	}

	public static boolean shouldRemindDownMap(Context context) {
		// int time = getPreferences(context).getInt(REMIND_DOWN_MAP_TIME, 0);
		// if (time > Constants.REMIND_DOWN_MAP_TIME) {
		// return false;
		// } else {
		// getPreferences(context).edit().putInt(REMIND_DOWN_MAP_TIME, time +
		// 1).commit();
		// return true;
		// }
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		int time = recordInfo.getRemindDownMapTime();
		if (time > Constants.REMIND_DOWN_MAP_TIME) {
			return false;
		} else {
			ContentValues values = new ContentValues();
			values.put("reminddownmaptime", time + 1);
			DataSupport.update(RecordInfo.class, values, recordInfo.getId());
			return true;
		}
	}

	public static void saveLoginStatus(Context context, boolean isLogin) {
		// getPreferences(context).edit().putBoolean(ISLOGIN, isLogin).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setLogin(isLogin);
		recordInfo.save();
	}

	public static void saveLastSelectedDevice(String lastSelectedDevice) {
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setLastSelectedDevice(lastSelectedDevice);
		recordInfo.save();
	}

	public static String getLastSelectedDevice() {
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.getLastSelectedDevice();
	}

	public static boolean isLogin(Context context) {
		// return getPreferences(context).getBoolean(ISLOGIN, false);
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.isLogin();
	}

	public static void saveFromPush(Context context, boolean fromPush) {
		// getPreferences(context).edit().putBoolean(FROM_PUSH,
		// fromPush).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setFromPush(fromPush);
		recordInfo.save();
	}

	public static boolean isFromPush(Context context) {
		// return getPreferences(context).getBoolean(FROM_PUSH, false);
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.isFromPush();
	}

	public static void saveCidStatus(Context context, boolean isCidBind) {
		// getPreferences(context).edit().putBoolean(IS_BIND_CID,
		// isCidBind).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setBindCid(isCidBind);
		recordInfo.save();
	}

	public static boolean isBindCid(Context context) {
		// return getPreferences(context).getBoolean(IS_BIND_CID, false);
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.isBindCid();
	}

	public static void saveLastLoginUser(Context context, String lastLoginUser) {
		// getPreferences(context).edit().putString(LAST_LOGIN_USER,
		// lastLoginUser).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setLastLoginUser(lastLoginUser);
		recordInfo.save();
	}

	public static String getLastLoginUser(Context context) {
		// return getPreferences(context).getString(LAST_LOGIN_USER, "");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String lastLoginUser = recordInfo.getLastLoginUser();
		if (!TextUtils.isEmpty(lastLoginUser)) {
			return lastLoginUser;
		}
		return "";
	}

	public static final String DBL_DEVICE_FANGDIU = "dbl_device_fangdiu_";
	public static final String DBL_DEVICE_FANGDIU_JULI = "dbl_device_fangdiu_juli_";
	public static final String DBL_DEVICE_FANGDIU_DUANKAI = "dbl_device_fangdiu_duankai_";

	public static void saveDefaultMacInfo(String mac) {
		try {
			List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("mac = ?", mac).find(DBLDeviceInfo.class);
			if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
				return;
			} else {
				DBLDeviceInfo dBLDeviceInfo = new DBLDeviceInfo();
				dBLDeviceInfo.setMac(mac);
				dBLDeviceInfo.setFangdiu(true);
				dBLDeviceInfo.setFangdiuJuli(Constants.DEFAULT_FANGDIU_JULI);
				dBLDeviceInfo.setFangdiuDuanKai(true);
				dBLDeviceInfo.save();
			}
		} catch (Exception e) {
		}
	}

	public static void saveBleDeviceFangdiu(Context context, String mac, boolean checked) {
		// getPreferences(context).edit().putBoolean(DBL_DEVICE_FANGDIU + mac,
		// checked).commit();
		try {
			List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("mac = ?", mac).find(DBLDeviceInfo.class);
			if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
				DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(0);
				ContentValues values = new ContentValues();
				values.put("fangdiu", checked);
				DataSupport.update(DBLDeviceInfo.class, values, dBLDeviceInfo.getId());
			} else {
				DBLDeviceInfo dBLDeviceInfo = new DBLDeviceInfo();
				dBLDeviceInfo.setMac(mac);
				dBLDeviceInfo.setFangdiu(checked);

				dBLDeviceInfo.setFangdiuJuli(Constants.DEFAULT_FANGDIU_JULI);
				dBLDeviceInfo.setFangdiuDuanKai(true);

				dBLDeviceInfo.save();
			}
		} catch (Exception e) {
		}
	}

	public static boolean getBleDeviceFangdiu(Context context, String mac) {
		// return getPreferences(context).getBoolean(DBL_DEVICE_FANGDIU + mac,
		// true);
		try {
			List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("mac = ?", mac).find(DBLDeviceInfo.class);
			if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
				DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(0);
				return dBLDeviceInfo.isFangdiu();
			}
		} catch (Exception e) {
			DBLDebug.loge("get bledevice fangdiu error\n" + e.getMessage());
			return true;
		}
		return true;
	}

	public static void saveBleDeviceFangdiuJuli(Context context, String mac, int juli) {
		// getPreferences(context).edit().putInt(DBL_DEVICE_FANGDIU_JULI + mac,
		// juli).commit();
		List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("mac = ?", mac).find(DBLDeviceInfo.class);
		if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
			DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(0);
			ContentValues values = new ContentValues();
			values.put("fangdiujuli", juli);
			DataSupport.update(DBLDeviceInfo.class, values, dBLDeviceInfo.getId());
		} else {
			DBLDeviceInfo dBLDeviceInfo = new DBLDeviceInfo();
			dBLDeviceInfo.setMac(mac);
			dBLDeviceInfo.setFangdiuJuli(juli);

			dBLDeviceInfo.setFangdiuDuanKai(true);
			dBLDeviceInfo.setFangdiu(true);

			dBLDeviceInfo.save();
		}
	}

	public static int getBleDeviceFangdiuJuli(Context context, String mac) {
		// return getPreferences(context).getInt(DBL_DEVICE_FANGDIU_JULI + mac,
		// Constants.DEFAULT_FANGDIU_JULI);
		try {
			List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("mac = ?", mac).find(DBLDeviceInfo.class);
			if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
				DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(0);
				return dBLDeviceInfo.getFangdiuJuli();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Constants.DEFAULT_FANGDIU_JULI;
		}
		return Constants.DEFAULT_FANGDIU_JULI;
	}

	public static void saveBleDeviceFangdiuDuankai(Context context, String mac, boolean checked) {
		// getPreferences(context).edit().putBoolean(DBL_DEVICE_FANGDIU_DUANKAI
		// + mac, checked).commit();
		List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("mac = ?", mac).find(DBLDeviceInfo.class);
		if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
			DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(0);
			ContentValues values = new ContentValues();
			values.put("fangdiuduankai", checked);
			DataSupport.update(DBLDeviceInfo.class, values, dBLDeviceInfo.getId());
		} else {
			DBLDeviceInfo dBLDeviceInfo = new DBLDeviceInfo();
			dBLDeviceInfo.setMac(mac);
			dBLDeviceInfo.setFangdiuDuanKai(checked);

			dBLDeviceInfo.setFangdiuJuli(Constants.DEFAULT_FANGDIU_JULI);
			dBLDeviceInfo.setFangdiu(true);

			dBLDeviceInfo.save();
		}
	}

	public static boolean getBleDeviceFangdiuDuankai(Context context, String mac) {
		// return getPreferences(context).getBoolean(DBL_DEVICE_FANGDIU_DUANKAI
		// + mac, true);
		try {
			List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("mac = ?", mac).find(DBLDeviceInfo.class);
			if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
				DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(0);
				return dBLDeviceInfo.isFangdiuDuanKai();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
		return true;
	}

	public static void saveImeiForMac(Context context, String mac, String imei) {
		// 清除之前 imei 对应 mac 的关系
		List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("imei = ?", imei).find(DBLDeviceInfo.class);
		if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
			for (int i = 0; i < dBLDeviceInfoList.size(); i++) {
				DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(i);
				ContentValues values = new ContentValues();
				values.put("imei", "");
				DataSupport.update(DBLDeviceInfo.class, values, dBLDeviceInfo.getId());
			}
		}

		saveMacForImei(context, imei, mac);
	}

	private static void saveMacForImei(Context context, String imei, String mac) {
		//fix 更换蓝牙配对时名字没有释放    →   ???????????
		List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("mac = ?", mac).find(DBLDeviceInfo.class);
		if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
			DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(0);
			ContentValues values = new ContentValues();
			values.put("imei", imei);
			DataSupport.update(DBLDeviceInfo.class, values, dBLDeviceInfo.getId());
		} else {
			DBLDeviceInfo dBLDeviceInfo = new DBLDeviceInfo();
			dBLDeviceInfo.setMac(mac);
			dBLDeviceInfo.setImei(imei);
			dBLDeviceInfo.save();
		}
	}

	public static String getImeiForMac(String mac) {
		// return getPreferences(context).getString(IMEI_FOR_MAC + mac, "");
		try {
			List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("mac = ?", mac).find(DBLDeviceInfo.class);
			if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
				DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(0);
				String imei = dBLDeviceInfo.getImei();
				if (!TextUtils.isEmpty(imei)) {
					return imei;
				}
			}
		} catch (Exception e) {
			return "";
		}
		return "";
	}

	public static String getMacForImei(Context context, String imei) {
		// return getPreferences(context).getString(MAC_FOR_IMEI + imei, "");
		List<DBLDeviceInfo> dBLDeviceInfoList = DataSupport.where("imei = ?", imei).find(DBLDeviceInfo.class);
		if (dBLDeviceInfoList != null && dBLDeviceInfoList.size() > 0) {
			DBLDeviceInfo dBLDeviceInfo = dBLDeviceInfoList.get(0);
			String mac = dBLDeviceInfo.getMac();
			if (!TextUtils.isEmpty(mac)) {
				return mac;
			}
		}
		return "";
	}

	public static void saveRebindImei(Context context, String imei) {
		// getPreferences(context).edit().putString(REBIND_IMEI, imei).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setRebindImei(imei);
		recordInfo.save();
	}

	public static String getRebindImei(Context context) {
		// return getPreferences(context).getString(REBIND_IMEI, "");
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		String imei = recordInfo.getRebindImei();
		if (!TextUtils.isEmpty(imei)) {
			return imei;
		}
		return "";
	}

	public static void saveRegisterToLogin(Context context, boolean flag) {
		// getPreferences(context).edit().putBoolean(REGSITER_TO_LOGIN,
		// flag).commit();
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		recordInfo.setRegsiterToLogin(flag);
		recordInfo.save();
	}

	public static boolean getRegisterToLogin(Context context) {
		// return getPreferences(context).getBoolean(REGSITER_TO_LOGIN, false);
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.isRegsiterToLogin();
	}

	public static void clearLoginParams(String username) {
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		ContentValues values = new ContentValues();
		values.put("isautologin", false);
		values.put("islogin", false);
		values.put("lastloginuser", username);
		DataSupport.update(RecordInfo.class, values, recordInfo.getId());
	}

	public static boolean getIs2RemindBleOpen() {
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		DBLDebug.logd("Prefernces getIs2RemindBleOpen: recordInfo" + recordInfo);
		if (recordInfo == null) {
			return true;
		}
		
		return recordInfo.isRemindBleOpen();
	}

	public static void saveIs2RemindBleOpen(boolean is2Remind) {
		try {
			RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
			if(recordInfo != null){
				ContentValues values = new ContentValues();
				values.put("remindBleOpen", is2Remind);
				DataSupport.update(RecordInfo.class, values, recordInfo.getId());
			} else {
				recordInfo = new RecordInfo();
				recordInfo.setRemindBleOpen(is2Remind);
				recordInfo.save();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static void saveGuaQiTime(long time) {
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		if(recordInfo != null){
			ContentValues values = new ContentValues();
			values.put("guaiQiTime", time);
			DataSupport.update(RecordInfo.class, values, recordInfo.getId());
		} else {
			recordInfo = new RecordInfo();
			recordInfo.setGuaiQiTime(time);
			recordInfo.save();
		}
	}
	
	public static long getGuaQiTime(){
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.getGuaiQiTime();
	}
	
	public static void saveStartFromBack(boolean isFromBack) {
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		if(recordInfo != null){
			ContentValues values = new ContentValues();
			values.put("startFromBack", isFromBack);
			DataSupport.update(RecordInfo.class, values, recordInfo.getId());
		} else {
			recordInfo = new RecordInfo();
			recordInfo.setStartFromBack(isFromBack);
			recordInfo.save();
		}
	}
	
	public static boolean getStartFromBack(){
		RecordInfo recordInfo = DataSupport.findFirst(RecordInfo.class);
		return recordInfo.isStartFromBack();
	}
}
